package cn.dxh.stagefinal.controller;
import cn.dxh.stagefinal.bean.Provider;
import cn.dxh.stagefinal.config.PageConfig;
import cn.dxh.stagefinal.service.ProviderService;
import cn.dxh.stagefinal.utils.ProviderExcelUtils;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletResponse;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * IntelliJ IDEA -> stagefinal
 * 2019/12/11 14:24
 *
 * @author zmyao
 * 别看，我是渣渣。
 */
@Controller
public class ProviderController {

    @Autowired
    private ProviderService providerService;
    @RequestMapping("/record/selectproviderlist")
    public String selectemployee(@RequestParam(defaultValue = "1") Integer index , String  pName ,String pLinkMan, ModelMap map){
        if (index == 0 ){
            index = 1;
        }
        PageInfo<Provider> propage = providerService.findProviders(index, PageConfig.PAGE_SIZE, pName, pLinkMan);
        map.addAttribute("propage",propage);
//        map.addAttribute("index",index);
        map.addAttribute("pName",pName);
        map.addAttribute("pLinkMan",pLinkMan);
        return  "/record/providerlist";
    }

    @RequestMapping("/record/insertprovider.html")
    public String insertpro(){
        return  "/record/insertprovider";
    }
    @RequestMapping("/record/insertprovider")
    public void  insertprovider(Provider provider, HttpServletResponse response){
        try {
            int len = providerService.insertProvider(provider);
            response.setContentType("text/html;charset= utf-8");
            PrintWriter out= response.getWriter();
            if (len > 0 ){
                out.println("<script> alert('供应商添加成功');location.href='/record/selectproviderlist'</script>");
            }
            else  {
                out.println("<script> alert('供应商添加失败');location.href='/record/insertprovider.html';</script>");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

@RequestMapping("/record/updateprovider.html")
    public String updatepro(Integer pId,ModelMap map){
    Provider provider = providerService.findProviderByPid(pId);
    map.addAttribute("provider",provider);
    return  "/record/updateprovider";
}
@RequestMapping("/record/updateprovider")
    public void  updateprovider(Provider provider,HttpServletResponse response){
    try {
        int len = providerService.updateProvider(provider);
        Integer pId = provider.getpId();
        response.setContentType("text/html;charset= utf-8");
        PrintWriter out= response.getWriter();
        if (len > 0 ){
            out.println("<script> alert('供应商修改成功');location.href='/record/selectproviderlist'</script>");
        }
        else  {
            out.println("<script> alert('供应商修改失败');location.href='/record/updateprovider.html?pId="+pId+"';</script>");
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

    @RequestMapping("/record/selectproviderone.html")
    public String selectproviderone(Integer pId,ModelMap map){
        Provider provider = providerService.findProviderByPid(pId);
        map.addAttribute("provider",provider);
        return  "/record/selectproviderone";
    }

    @RequestMapping("/record/deletprovider.html")
    public void deletprovider(Integer pId,HttpServletResponse response){
        try {
            int len = providerService.deleteProvider(pId, 0);
            response.setContentType("text/html;charset= utf-8");
            PrintWriter out= response.getWriter();
            if (len > 0 ){
                out.println("<script> alert('供应商删除成功');location.href='/record/selectproviderlist'</script>");
            }
            else  {
                out.println("<script> alert('供应商删除失败');location.href='/record/selectproviderlist';</script>");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    @RequestMapping("/record/deleteanddaochuprovider")
    public void deleteanddaochu(String method,int[] pIds,HttpServletResponse response){
        try {
            response.setContentType("text/html;charset=utf-8");
            PrintWriter out = response.getWriter();
            if ("delete".equals(method)){
                int len = providerService.deleteProviderByPids(pIds);
                if (len > 0 ){
                    out.println("<script> alert('全部删除成功');location.href='/record/selectproviderlist';</script>");
                }
                out.println("<script> alert('删除失败');location.href='/record/selectproviderlist';</script>");
            }
            else  if ("daochu".equals(method)){

                List<Provider> providers = providerService.findProvidersByPid(pIds);
                ProviderExcelUtils.createFirst("供应商信息");
                ProviderExcelUtils.createOthers(providers);
                SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
                ProviderExcelUtils.export(new FileOutputStream("d:\\javaCreate\\"+"provider"+(format.format(new Date()))+".xls"));
                out.println("<script> alert('导出成功');location.href='/record/selectproviderlist';</script>");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
